# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        stack=[root]
        result=[root.val]
        temp=root
        while temp.left:
            result.append(temp.left.val)
            stack.append(temp.left)
            temp=temp.left
        while len(stack)!=0:
            temp=stack.pop()
            if temp.right:
                stack.append(temp.right)
                result.append(temp.right.val)
                temp=temp.right
                while temp.left:
                    result.append(temp.left.val)
                    stack.append(temp.left)
                    temp=temp.left
        return result